package com.baihan.angus.app.game.busshow.pop_dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.AppUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.adps.QuestionAnswerAdp;
import com.baihan.angus.app.game.busshow.adps.VoteResultAdp;

import java.util.ArrayList;
import java.util.List;

public class QuestionAndVoteDialog extends Dialog {

    private Context mContext;
    private WindowManager.LayoutParams mLayoutParams;
    private LinearLayout llClose, llVote;
    private TextView tvTitle, tvAnswer;
    private RadioGroup rgSelect;
    private RadioButton rbQuestion, rbVote;
    private RecyclerView rvQuestions, rvVote;

    private QuestionAnswerAdp mQuestionAnswerAdp;
    private VoteResultAdp mVoteResultAdp;
    private List<String> questionList = new ArrayList<>();
    private List<String> voteList = new ArrayList<>();

    public QuestionAndVoteDialog(@NonNull Context context) {
        super(context, R.style.FullDialog);
        this.mContext = context;
        initContentView();
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        Window dialogWindow = this.getWindow();
        mLayoutParams = dialogWindow.getAttributes();
        mLayoutParams.width = AppUtil.getScreenWidth(context);
        mLayoutParams.height = AppUtil.getScreenHeight(context);
        dialogWindow.setAttributes(mLayoutParams);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 30);
        dialogWindow.getDecorView().setBackgroundResource(R.color.dialog_bg);
        dialogWindow.setGravity(Gravity.BOTTOM);
    }

    private void initContentView() {
        View dialogView = LayoutInflater.from(this.getContext()).inflate(R.layout.dialog_question_and_vote, (ViewGroup) null);
        setContentView(dialogView);
        llClose = dialogView.findViewById(R.id.llClose);
        llVote = dialogView.findViewById(R.id.llVote);
        tvTitle = dialogView.findViewById(R.id.tvTitle);
        tvAnswer = dialogView.findViewById(R.id.tvAnswer);
        rgSelect = dialogView.findViewById(R.id.rgSelect);
        rbQuestion = dialogView.findViewById(R.id.rbQuestion);
        rbVote = dialogView.findViewById(R.id.rbVote);
        rvQuestions = dialogView.findViewById(R.id.rvQuestions);
        rvVote = dialogView.findViewById(R.id.rvVote);

        mQuestionAnswerAdp = new QuestionAnswerAdp();
        rvQuestions.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mQuestionAnswerAdp.bindToRecycleView(rvQuestions);

        mVoteResultAdp = new VoteResultAdp();
        rvVote.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        mVoteResultAdp.bindToRecycleView(rvVote);

        rgSelect.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbQuestion) {
                    rvQuestions.setVisibility(View.VISIBLE);
                    llVote.setVisibility(View.GONE);
                } else if (checkedId == R.id.rbVote) {
                    rvQuestions.setVisibility(View.GONE);
                    llVote.setVisibility(View.VISIBLE);
                }
            }
        });

        llClose.setOnClickListener(v -> {
            cancel();
        });
    }

    public void initData() {
        for (int i = 0; i < 5; i++) {
            questionList.add(new String());
            voteList.add(new String());
        }
        mQuestionAnswerAdp.addAll(questionList);
        mVoteResultAdp.addAll(voteList);
    }
}
